﻿Imports System.Data
Partial Class WebForm_ManageRole_OpenManageChannelMapping
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Me.BindDDLSignature()
            Me.BindDDLChannel()
        End If
    End Sub

    Protected Sub chkSelect_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim RowIndex As Integer = DirectCast(DirectCast(sender, CheckBox).NamingContainer, GridViewRow).RowIndex
        Dim Division As String = Me.gv.DataKeys(RowIndex).Item("Division").ToString()
        Dim Signature As String = Me.gv.DataKeys(RowIndex).Item("Signature").ToString()
        Dim Channel As String = Me.gv.DataKeys(RowIndex).Item("Channel").ToString()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.Length = 0
        sql.AppendFormat("delete from RoleChannelMapping ")
        sql.AppendFormat("where RoleID='{0}' ", Me.RoleID)
        sql.AppendFormat("and Division='{0}' ", Division)
        sql.AppendFormat("and Signature='{0}' ", Signature.Replace("'", "''"))
        sql.AppendFormat("and Channel='{0}' ", Channel.Replace("'", "''"))
        If DirectCast(sender, CheckBox).Checked = True Then
            sql.AppendFormat(";insert into RoleChannelMapping(RoleID,Division,Signature,Channel) ")
            sql.AppendFormat("Select ")
            sql.AppendFormat("'{0}' ", Me.RoleID)
            sql.AppendFormat(",'{0}' ", Division)
            sql.AppendFormat(",'{0}' ", Signature.Replace("'", "''"))
            sql.AppendFormat(",'{0}' ", Channel.Replace("'", "''"))
        End If
        'MyObj.WriteLog(sql.ToString())
        dat.AccData(sql.ToString())
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.InitDataAndShow()
    End Sub

    Protected Sub gv_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gv.PageIndexChanged
        Me.mpSearch.Show()
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gv.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then

        End If
    End Sub

    Protected Sub btnQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnQuery.Click
        Me.InitDataAndShow()
    End Sub

    Private Sub BindDDLSignature()
        Dim ddlControl As DropDownList = Me.ddlSignature
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select distinct a.Signature,b.Description ")
        sql.AppendFormat("from ChannelView a ")
        sql.AppendFormat("join ProductHierarchyOriginal b ")
        sql.AppendFormat("on a.Signature=b.Code and b.LevelName='signature' ")
        sql.AppendFormat("where a.Division ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select Division from Role where RoleID ")
        sql.AppendFormat("	in ")
        sql.AppendFormat("	( ")
        sql.AppendFormat("		select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat("	) ")
        sql.AppendFormat(")")
        'MyObj.WriteLog(sql.ToString())
        dt = dat.GetData(sql.ToString())

        ddlControl.DataTextField = "Description"
        ddlControl.DataValueField = "Signature"
        ddlControl.DataSource = dt
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Private Sub BindDDLChannel()
        Dim ddlControl As DropDownList = Me.ddlChannel
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select distinct a.Channel,a.ChannelName ")
        sql.AppendFormat("from ChannelView a ")
        sql.AppendFormat("join ProductHierarchyOriginal b ")
        sql.AppendFormat("on a.Signature=b.Code and b.LevelName='signature' ")
        sql.AppendFormat("where a.Division ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select Division from Role where RoleID ")
        sql.AppendFormat("	in ")
        sql.AppendFormat("	( ")
        sql.AppendFormat("		select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat("	) ")
        sql.AppendFormat(")")
        sql.AppendFormat("order by a.ChannelName ")
        'MyObj.WriteLog(sql.ToString())
        dt = dat.GetData(sql.ToString())

        ddlControl.DataTextField = "ChannelName"
        ddlControl.DataValueField = "Channel"
        ddlControl.DataSource = dt
        ddlControl.DataBind()
        ddlControl.Items.Insert(0, MyObj.GetDDLSelectAllItem())
    End Sub

    Public Sub InitData()
        Me.BindDDLSignature()
        Me.BindDDLChannel()
        Me.InitDataAndShow()
    End Sub

    Public Sub InitDataAndShow()
        Me.mpSearch.Show()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("select distinct a.* ")
        sql.AppendFormat(",b.Description as SignatureText ")
        sql.AppendFormat(",Case isnull(c.RoleID,'') when '' then '0' else '1' end CanAccess ")
        sql.AppendFormat("from ChannelView a ")
        sql.AppendFormat("join ProductHierarchyOriginal b ")
        sql.AppendFormat("on a.Signature=b.Code and b.LevelName='signature' ")
        sql.AppendFormat("left join RoleChannelMapping c ")
        sql.AppendFormat("on a.Division=c.Division and a.Signature=c.Signature ")
        sql.AppendFormat("and a.Channel=c.Channel ")
        sql.AppendFormat("and c.RoleID ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat(") ")
        sql.AppendFormat("join RoleHierarchyMapping d ")
        sql.AppendFormat("on a.Signature=d.ProdHierarchy and Len(d.ProdHierarchy)=2 ")
        sql.AppendFormat("and d.RoleID ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat(") ")
        sql.AppendFormat("where a.Division ")
        sql.AppendFormat("in ")
        sql.AppendFormat("( ")
        sql.AppendFormat("	select Division from Role where RoleID ")
        sql.AppendFormat("	in ")
        sql.AppendFormat("	( ")
        sql.AppendFormat("		select '{0}' union select Value from dbo.fn_GetAllPapasByRoleID('{0}') ", Me.RoleID)
        sql.AppendFormat("	) ")
        sql.AppendFormat(") ")
        If Me.ddlSignature.SelectedValue <> "" Then
            sql.AppendFormat("and a.Signature='{0}' ", Me.ddlSignature.SelectedValue)
        End If
        If Me.ddlChannel.SelectedValue <> "" Then
            sql.AppendFormat("and a.Channel='{0}' ", Me.ddlChannel.SelectedValue)
        End If
        sql.AppendFormat("order by b.Description,a.ChannelName ")
        'MyObj.WriteLog(sql.ToString())
        Me.gv.DataSource = dat.GetData(sql.ToString())
        Me.gv.DataBind()

    End Sub

    Protected Function BooleanConverter(ByVal strTMP As Object) As Boolean
        Return (IIf(strTMP.ToString() = "1", True, False))
    End Function

    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property

End Class
